import { Component, OnInit,Input,Output,EventEmitter} from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../../shared/smartadmin.config';
declare var $: any;
@Component({
  	selector: 'addproblem',
  	templateUrl: './problem.component.html',
  	styleUrls:['./problem.css'],
})
export class ProblemComponent implements OnInit{
	constructor(private jsonApiService: JsonApiService) {}
	@Input() deviceId:string;
	@Input() carNumber:string;
	isDisable=true;
	proTypeOptions=[];//存储问题记录分类选项描述
	proSlectedItem:any;//记录选中一类时的数据,渲染页面选项
	proTypeParams:any={		//存储用户操作选择的数据
		problemType:'',			//问题类型
		problemClass:'',		//问题分类
		phenomenon:'',			//问题现象
		repairGuide:'',			//维修指导
		repairWay:'',			//维修方式
		pheDescribe:'',			//现象描述
	}
	seneorPhenlist=[];
	
	hideOrshowline=[];
	legendSleced:any={};
	
	ngOnInit() {
		
		this.initProTypeOptions();
	}
  	ngOnChanges() {
//		console.log(this.carNumber,this.deviceId)
  	}
	startTime_problem:string='';
  	endTime_problem:string='';
  	isPush='0';
	initProTypeOptions(){
		this.proTypeOptions=problemOptions.initProTypeOptions;
		
	}
	//监听问题类型改变事件
	problemTypeChange(){
		if(this.proTypeParams.problemType=='') return;
		let sort=+this.proTypeParams.problemType;
		this.proSlectedItem=this.proTypeOptions[sort];
		this.proTypeParams.problemClass='';
		this.problemClassChange();
	}
	//监听问题分类改变事件
	pcIsOther=false;
	otherInpMess='';
	problemClassChange(){
		this.seneorPhenlist=[];
		this.proTypeParams.phenomenon='';
		this.otherInpMess='';
		this.isPush='0';
//		判断选的是不是其他
		let isOther=this.proSlectedItem.childs[+this.proTypeParams.problemClass].name;
		if(isOther=='其他'){
			this.pcIsOther=true;
		}else{
			this.pcIsOther=false;
		}
		if(this.proTypeParams.problemType=="1"&&this.proTypeParams.problemClass=="1"){
			
		}
		else{
			this.problemPhenChange();
		}
	}
	//监听问题现象改变事件
	problemPhenChange(){
		this.proTypeParams.repairGuide='';
		this.proTypeParams.repairWay='';
		this.proTypeParams.pheDescribe='';
	}
	addseneorPhenItem(){
		let sensor=$('#sensorPhenName').val();
		if(!sensor||!this.proTypeParams.phenomenon){
			this.jsonApiService.message('请选择传感器问题现象');
			return;
		}
		let len=this.seneorPhenlist.length;
		let itemId=1;
		if(len>0){
			itemId=this.seneorPhenlist[len-1].index+1;
		}
			
		let phen=this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name;
		let isAdd=true;
		for(let item of this.seneorPhenlist){
			if(item.phenomenon==phen){
				let str='传感器'+sensor;
				if(str==item.sensorName){
					isAdd=false;
					break;
				}
			}
		}
		if(isAdd){
			let pheItem={
				index:itemId,
				sensorName:'传感器'+sensor,
				phenomenon:phen,
			}
			this.seneorPhenlist.push(pheItem);
		}else{
			this.jsonApiService.message('现象已存在,不需要重复添加');
		}
		
		
	}
	delSeneorPhenItem(index){
		this.jsonApiService.confirm_message("确定删除该条现象?").then(()=>{
			this.seneorPhenlist.splice(index,1);
		})
	}
	
	getSelectedVAL(){
		var result:any={
			problemTypeStr:this.proSlectedItem.name, //问题类型
			problemClassStr:'',
			phenomenonStr:'',
			repairGuideStr:'',
			repairWayStr:'',
			pheDescribeStr:'',
			resultPhenStr:'',
		}
		if(!this.proSlectedItem){
			this.jsonApiService.message('请选择问题类型');
			return result;
		};
		if(this.proTypeParams.problemClass){
			//问题分类
			result.problemClassStr=this.proSlectedItem.childs[+this.proTypeParams.problemClass].name;  
		}
		if(this.proTypeParams.phenomenon){
			//问题现象
			result.phenomenonStr  =this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name;
			result.resultPhenStr=result.phenomenonStr;
		}
		if(this.proTypeParams.repairGuide){
			//维修指导
			result.repairGuideStr =this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].repairGuide[+this.proTypeParams.repairGuide].name;
		}
		if(this.proTypeParams.repairWay){
			//维修方式
			result.repairWayStr   =this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].repairWay[+this.proTypeParams.repairWay].name;
		}
		if(this.proTypeParams.pheDescribe){
			//维修描述
			result.pheDescribeStr =this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].pheDescribe[+this.proTypeParams.pheDescribe].name;
		}
		
		if(this.seneorPhenlist.length>0){
			let list=[];
			for(let item of this.seneorPhenlist){
				let str=item.sensorName+'-'+item.phenomenon;
				list.push(str)
			}
			result.resultPhenStr=list.join(',');
			
		}
		if(this.pcIsOther){
			result.resultPhenStr=this.otherInpMess;
		}
		return result;
		
	}
	
  	submitRecord(){
		if(!this.deviceId){
			this.jsonApiService.message('先选择车辆...');
			return;
		}
		if(!this.proTypeParams.problemType){
			this.jsonApiService.message('请选择问题分类');
			return;
		}
		if(!this.startTime_problem||!this.endTime_problem){
			this.jsonApiService.message('问题起始时间不能为空...');
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime_problem,this.endTime_problem,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let operaParam=this.getSelectedVAL();
		if(this.proTypeParams.problemType=='1'&&this.proTypeParams.problemClass=='1'){
			let sensor=$('#sensorPhenName').val();
			if(this.seneorPhenlist.length<1&&!sensor&&!this.proTypeParams.phenomenon) {
				this.jsonApiService.message('请确认添加传感器问题现象');
				return;
			}
			if(sensor&&this.proTypeParams.phenomenon){
				let phen=this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name;
				let phenResult='传感器'+sensor+'-'+phen;
				if(this.seneorPhenlist.length<1){
					operaParam.resultPhenStr=phenResult;
				}
				else{
					let findIndex=operaParam.resultPhenStr.indexOf(phenResult)
					if(findIndex<0){
						operaParam.resultPhenStr=operaParam.resultPhenStr+','+phenResult;
					}
				}
				
			}
		}
		if(!this.proTypeParams.problemType||!this.proTypeParams.problemClass) {
			this.jsonApiService.message('请选择问题分类');
			return;
		}
		if(operaParam.resultPhenStr=='') {
			this.jsonApiService.message('问题现象不能为空');
			return;
		}
		let proType=(+this.proTypeParams.problemType+1)+'0'+(+this.proTypeParams.problemClass+1);
		if(operaParam.problemClassStr=='其他'){
			proType=(+this.proTypeParams.problemType+1)+'00';
		}
		this.jsonApiService.http("realtime/getRealTimeData.html", 'deviceId='+this.deviceId).subscribe(result => {
			let params={
				programType:proType,
				deviceId:this.deviceId,
				version: result.data.version,
				startDate:this.startTime_problem,
				endDate:this.endTime_problem,
				reason:operaParam.resultPhenStr,
				exceptionType:+this.proTypeParams.problemType+1,
				repairedGuide:operaParam.repairGuideStr,
				isPush:this.isPush,
				coefficient:result.data.coef
			}
			let reqdata='';
			for(let keys in params){
				reqdata=reqdata+keys+'='+params[keys]+'&';
			}
			reqdata=reqdata.substring(0,reqdata.length-1);
			this.jsonApiService.http("dataExceptionRecord/addDataException.html", reqdata).subscribe(result2 => {
				this.jsonApiService.message(result2.message);
				if(result2.success){
					this.isDisable=false;
//					this.load();
				}
			})
		})
  	}
	//提交到设备故障记录
	submitDevice(){
		if(!this.deviceId){
  			this.jsonApiService.message('先选择车辆...');
  			return;
  		}
		if(!this.startTime_problem||!this.endTime_problem){
			this.jsonApiService.message('问题起始时间不能为空...');
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime_problem,this.endTime_problem,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let operaParam=this.getSelectedVAL();
//		if(this.proTypeParams.problemType=='1'&&this.proTypeParams.problemClass=='1'){
//			if(this.seneorPhenlist.length<1){
//				this.jsonApiService.message('请确认添加传感器问题现象');
//				return;
//			}
//		}
		if(this.proTypeParams.problemType=='1'&&this.proTypeParams.problemClass=='1'){
			let sensor=$('#sensorPhenName').val();
			if(this.seneorPhenlist.length<1&&!sensor&&!this.proTypeParams.phenomenon) {
				this.jsonApiService.message('请确认添加传感器问题现象');
				return;
			}
			if(sensor&&this.proTypeParams.phenomenon){
				let phen=this.proSlectedItem.childs[+this.proTypeParams.problemClass].phenomenon[+this.proTypeParams.phenomenon].name;
				let phenResult='传感器'+sensor+'-'+phen;
				if(this.seneorPhenlist.length<1){
					operaParam.resultPhenStr=phenResult;
				}
				else{
					let findIndex=operaParam.resultPhenStr.indexOf(phenResult)
					if(findIndex<0){
						operaParam.resultPhenStr=operaParam.resultPhenStr+','+phenResult;
					}
				}
				
			}
		}
		if(!this.proTypeParams.problemType||!this.proTypeParams.problemClass) {
			this.jsonApiService.message('请选择问题分类');
			return;
		}
		let proType=(+this.proTypeParams.problemType+1)+'0'+(+this.proTypeParams.problemClass+1);
  		if(operaParam.resultPhenStr=='') {
			this.jsonApiService.message('问题现象不能为空');
			return;
		}
  		
		let params={
			programType:proType,
			deviceId:this.deviceId,             			//编号
			carNumber:this.carNumber,						//车牌
			faultTypeName:operaParam.problemClassStr,		//故障类型
			faultTime:this.startTime_problem,               //故障时间
			faultPhenomenon:operaParam.resultPhenStr,	    //故障现象:例  传感器1-平直
			isPush:this.isPush,								//是否推送
			repairedGuide:operaParam.repairGuideStr,		//维修指导
			repairedRecords:'',								//维修记录
			repairedStatus:'',				                //维修状态(故障状态)
			repairedTime:'',								//维修时间
			repairedUser:'',								//维修人员
			remark:'',										//备注
			repairedConclusion:'',							//维修总结
		}
		
		let reqdata='';
		for(let keys in params){
			reqdata=reqdata+keys+'='+params[keys]+'&';
		}
		reqdata=reqdata.substring(0,reqdata.length-1);
		this.jsonApiService.http('deviceRepaired/validateRecord.html', 'deviceId='+this.deviceId).subscribe(result => {
			if(result.valid){
				this.jsonApiService.confirm_message(result.message+'确认要继续提交?').then(()=>{
					this.jsonApiService.http('deviceRepaired/insertDeviceRepaired.html', reqdata).subscribe(result => {
						this.jsonApiService.message(result.message)
						if(result.success){
//							this.load();
						}
					})
				})
			}else{
				this.jsonApiService.http('deviceRepaired/insertDeviceRepaired.html', reqdata).subscribe(result => {
					this.jsonApiService.message(result.message)
					if(result.success){
//						this.load();
					}
				})
			}
		})
	}
	
}